package com.ruoyi.business.domain.jackyun;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.business.domain.koc.TbUserAddress;
import com.ruoyi.business.domain.product.TbOrder;
import com.ruoyi.business.domain.product.TbOrderItem;
import com.ruoyi.business.mapper.koc.TbUserAddressMapper;
import com.ruoyi.business.mapper.product.TbOrderItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class JackClent {
    @Autowired
     TbOrderItemMapper tbOrderItemMapper;
    @Autowired
     TbUserAddressMapper tbUserAddressMapper;
    @Value("${jackyun.appKey}")
    private String appKey;
    @Value("${jackyun.appSecret}")
    private String appSecret;
    private final JackyunOpenClientService clientService = new JackyunOpenClientService();
    public JackyunResponse omsTradeOrderCreate(TbOrder tbOrder) {
        List<TbOrderItem> tbOrderItems = tbOrderItemMapper.selectList(new LambdaQueryWrapper<>(TbOrderItem.class).eq(TbOrderItem::getOrderId, tbOrder.getId()));
        TbUserAddress tbUserAddress = tbUserAddressMapper.selectById(tbOrder.getAddressId());
        OmsTradeOrderCreateRequestBizData requestBizData = new OmsTradeOrderCreateRequestBizData();
        TradeOrderDTO tradeOrder =new TradeOrderDTO();
        tradeOrder.setTradeType((byte) 2);
        tradeOrder.setTradeTime(new Date());
        tradeOrder.setShopName("有赞悦哺会员商城");
        tradeOrder.setChargeCurrency("人民币");
        tradeOrder.setReceiverName(tbUserAddress.getReceiverName());
        tradeOrder.setPhone(tbUserAddress.getReceiverPhone());
        tradeOrder.setMobile(tbUserAddress.getReceiverPhone());
        tradeOrder.setState(tbUserAddress.getProvinceName());
        tradeOrder.setCity(tbUserAddress.getCityName());
        tradeOrder.setDistrict(tbUserAddress.getDistrictName());
        tradeOrder.setAddress(tbUserAddress.getDetailAddress());
        tradeOrder.setSellerMemo("小程序积分兑换测试");
        tradeOrder.setOnlineTradeNo(tbOrder.getOrderNo());
        List<TradeOrderDetail> tradeOrderDetails = new ArrayList<>();
        for (TbOrderItem tbOrderItem : tbOrderItems){
            TradeOrderDetail tradeOrderDetail=new TradeOrderDetail();
            tradeOrderDetail.setGoodsNo(tbOrderItem.getSkuCode());
            tradeOrderDetail.setSpecName(tbOrderItem.getSpecsText());
            tradeOrderDetail.setBarcode(tbOrderItem.getBarcode());
            tradeOrderDetail.setSellCount(new BigDecimal(tbOrderItem.getQuantity()));
            tradeOrderDetails.add(tradeOrderDetail);
        }
        tradeOrder.setTradeOrderDetails(tradeOrderDetails);
        requestBizData.setTradeOrder(tradeOrder);
        JackyunResponse jackyunResponse = getJackyunResponse(ApiEnum.OMSTRADEORDERCREATE.getValue(), requestBizData);
        return jackyunResponse;
    }
    public JackyunResponse omsTradeOrderCancel(List<String> erpNos) {
        OmsTradeOrderCancelRequestBizData requestBizData = new OmsTradeOrderCancelRequestBizData();
        requestBizData.setCancelReason("后台管理员取消");
        requestBizData.setTradeNos(erpNos.toArray(new String[0]));
        JackyunResponse jackyunResponse = getJackyunResponse(ApiEnum.OMSTRADEORDERCANCEL.getValue(), requestBizData);
        return jackyunResponse;
    }
    public JackyunResponse getJackyunResponse(String method, BaseRequestBizData requestBizData) {
        // 开放平台应用信息
        JackyunMemberInfo memberInfo = new JackyunMemberInfo();
        memberInfo.setAppKey(appKey);
        memberInfo.setAppSecret(appSecret);
        JackyunResponse jackyunResponse = this.clientService.call(method, requestBizData, memberInfo);
        return jackyunResponse;
    }

    public Boolean erpStockQuantityGet(TbOrder order) {
        JackyunResponse jackyunResponse=new JackyunResponse();
        List<TbOrderItem> tbOrderItems = tbOrderItemMapper.selectList(new LambdaQueryWrapper<>(TbOrderItem.class).eq(TbOrderItem::getOrderId, order.getId()));
        for (TbOrderItem tbOrderItem: tbOrderItems) {
            ErpStockQuantityGetRequestBizData requestBizData = new ErpStockQuantityGetRequestBizData();
            requestBizData.setSkuBarcode(tbOrderItem.getBarcode());
            // "warehouseCode": "0001",
            //"warehouseName": "悦哺销售仓",
            requestBizData.setWarehouseCode("0001");
            requestBizData.setPageSize(10);
            jackyunResponse = getJackyunResponse(ApiEnum.ERPSTOCKQUANTITYGET.getValue(), requestBizData);
            ErpStockQuantityGetResponseBizData responseBizData = null;
            if (jackyunResponse.isSuccess()) {
                JSONObject response = JsonUtils.deJson(jackyunResponse.toJson(), JSONObject.class);
                responseBizData = response.getJSONObject("result").getJSONObject("data").toJavaObject(ErpStockQuantityGetResponseBizData.class);
                if (responseBizData.getGoodsStockQuantity().get(0).getCurrentQuantity().longValue() < tbOrderItem.getQuantity()
                    ||responseBizData.getGoodsStockQuantity().get(0).getCurrentQuantity().longValue()<100) {
                    return false;
                }
            }else {
                return false;
            }

        }
        return true;
    }
}
